import java.util.ArrayList;


public class Stat1 {
	
	
	// relation
	// arraylist of tuples<a1,a2>
//	ArrayList<Tuple> r = new ArrayList<Tuple>();
	
	public final int high_a1;
	public final int low_a1;
	
	public final int high_a2;
	public final int low_a2;

	ArrayList<Tuple> r;
	private boolean verbose;
	
	public Stat1(ArrayList<Tuple> r, boolean verbose) {
		this.verbose = verbose;
		this.r = r;
		
		
		high_a1 = getHigh("A1");
		low_a1 = getLow("A1");
		
		high_a2 = getHigh("A2");
		low_a2 = getLow("A2");
	}

	/**
	 * Counts tuples in relation
	 * @return
	 */
	public int count() {
		return r.size();
	}
	
	/**
	 * Gets maximum value of an attribute in relation.
	 * @param attribute
	 * @return
	 */
	public int getHigh(String attr) {
		
		int max = 0;
		for (Tuple t : r) {
			int val = t.get(attr);
			if (val > max) {
				max = val;
			}
		}
		
		return max;
	}
	
	/**
	 * Fetch minimum of the attribute
	 * @param attribute
	 * @return
	 */
	public int getLow(String attr) {
		int min = 0;
		for (Tuple t : r) {
			int val = t.get(attr);
			if (val < min) {
				min = val;
			}
		}
		return min;
	}
	
	// r1 <= A1 <= s1
    // Col > value = (high - value) / (high - low)
	public double getReductionFactor(int R1, int S1, String attr) {
		double r1 = (double) R1;
		double s1 = (double) S1;
		report("Set1: High A1: " + getHigh("A1") + " , high A2: " + getHigh("A2"));
		report("Set1 : "+ attr + " reduction factor: (s1 - r1  + 1) / (high - low) =\n    " + "(" + s1 + " - " + r1 + "+ 1)" + 
				"/" + "(" + getHigh(attr) + "-" + getLow(attr) + ") = " + (s1 - r1) / (getHigh(attr) - getLow(attr)));
		return (s1 - r1 + 1) / (getHigh(attr) - getLow(attr));
	}

	private void report(String string) {
		if (verbose ) {
			System.out.println(string);
		}
	}
	
}
